<?php
class RP_Model_User_RecruiterMapper{
	protected $_dbTable;
    
    public function setDbTable ($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (! $dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }
    
    public function getDbTable ()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('RP_Model_DbTable_AuthUser');
        }
        return $this->_dbTable;
    }
	public function register(RP_Model_User_Recruiter $rec){
		
		try{
			$this->getDbTable()
                ->getAdapter()
                ->beginTransaction();
                
            $authData = array(
            		'user_type_id'=>$rec->getUser_type_id(),
					'username'=>$rec->getUsername(),
					'password'=>$rec->getPassword(),
					'email'=>$rec->getEmail(),
					);
            
            $pKey = $this->getDbTable()->insert($authData);
            $rec->setUser_id($pKey);
            
            $userRole = new RP_Model_DbTable_UserRole();
            $userRole->insert(
            array('user_id' => $rec->getUser_id(), 'role_id' => 'recruiter'));
            
            $recruiterData = array(
            				'user_id'=>$rec->getUser_id(),
            				'recruiter_name'=>$rec->getRecruiter_name(),
            				'recruiter_type'=>$rec->getRecruiter_type(),
            				'addressline'=>$rec->getAddressline(),
            				'city'=>$rec->getCity(),
            				'district'=>$rec->getDistrict(),
            				'state'=>$rec->getState(),
            				'contact_no'=>$rec->getContact_no(),
            				'alternate_no'=>$rec->getAlternate_no(),
            				'contact_person'=>$rec->getContact_person(),
            				'pincode'=> $rec->getPincode()
            			);
                        
            $recruiter = new RP_Model_DbTable_Recruiter();
            $recruiter->insert($recruiterData);
            
            $this->getDbTable()
                ->getAdapter()
                ->commit();
            
            
		}
		catch(Exception $e){
			$this->getDbTable()
                ->getAdapter()
                ->rollBack();             
            throw $e;
		}
		return $rec;
	}
    
}//End class
?>